/**
 * 
 */
package com.javass.jdbc.impl;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.javass.jdbc.DatabaseComment;

/**
 * @author zhuguojie，zhu915139481@126.com
 * @date 2015年11月11日 下午4:38:34
 * @version 1.0
 */
public abstract class AbstractDatabaseComment implements DatabaseComment {

	public static final String COLUMN_REMARKS = "REMARKS";
	protected Connection connection;

	public AbstractDatabaseComment(Connection connection) {
		super();
		this.connection = connection;
	}

	protected void closeConnection() throws SQLException {
		this.connection.close();
	}

	@Override
	public String getTableComment(String catalog, String schema, String tableName) throws SQLException {
		DatabaseMetaData databaseMetaData = connection.getMetaData();
		ResultSet tables = databaseMetaData.getTables(catalog, schema, tableName, new String[] { "TABLE" });
		String remarks = null;
		if (tables.next()) {
			remarks = tables.getString(COLUMN_REMARKS);
		}
		tables.close();
		return remarks;
	}

	@Override
	public String getColumnComment(String catalog, String schema, String tableName, String columnName) throws SQLException {
		DatabaseMetaData databaseMetaData = connection.getMetaData();
		ResultSet columns = databaseMetaData.getColumns(catalog, schema, tableName, columnName);
		String remarks = null;
		if (columns.next()) {
			remarks = columns.getString(COLUMN_REMARKS);
		}
		columns.close();
		return remarks;
	}
}
